package cn.edu.nju.software.cdiy.action.user.all;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;

import cn.edu.nju.software.cdiy.bean.Scheme;
import cn.edu.nju.software.cdiy.service.SchemeService;
import cn.edu.nju.software.cdiy.util.Constant;
import cn.edu.nju.software.cdiy.util.Item;
import cn.edu.nju.software.cdiy.util.SortType;
import cn.edu.nju.software.cdiy.util.SortWay;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

@SuppressWarnings("serial")
public class SchemeSearchAction extends ActionSupport implements SessionAware, ServletRequestAware, Preparable  {

	private Map<String, Object> session;
	private HttpServletRequest request;
	private SchemeService schemeService;
	private List<Integer> selectedBrands;
	private List<Integer> selectedPrices;
	private List<Integer> selectedTypes;
	private int sortOrder = Constant.TIME;
	private int pageNum = 1;
	
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	
	public void setSchemeService(SchemeService schemeService) {
		this.schemeService = schemeService;
	}

	public List<Integer> getSelectedBrands() {
		return selectedBrands;
	}

	public void setSelectedBrands(List<Integer> selectedBrands) {
		this.selectedBrands = selectedBrands;
	}

	public List<Integer> getSelectedPrices() {
		return selectedPrices;
	}

	public void setSelectedPrices(List<Integer> selectedPrices) {
		this.selectedPrices = selectedPrices;
	}

	public List<Integer> getSelectedTypes() {
		return selectedTypes;
	}

	public void setSelectedTypes(List<Integer> selectedTypes) {
		this.selectedTypes = selectedTypes;
	}

	public int getSortOrder() {
		return sortOrder;
	}

	public void setSortOrder(int sortOrder) {
		this.sortOrder = sortOrder;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	@Override
	public void prepare() throws Exception {
		if (selectedBrands != null) {
			selectedBrands.clear();
		}
		if (selectedPrices != null) {
			selectedPrices.clear();
		}
		if (selectedTypes != null) {
			selectedTypes.clear();
		}
	}

	@Override
	public String execute() throws Exception {
		if(!session.containsKey(Constant.S_SCHEME_FILTER)) {
			Map<String,List<Item>> filter = schemeService.getFilterBoard();
			session.put(Constant.S_SCHEME_FILTER, filter);
		}
		
		String sessionQueryString = (String) session.get(Constant.S_SCHEME_QUERY_STRING);
		String queryString = request.getQueryString();
		if (queryString == null) {
			queryString = "sortOrder=1&pageNum=1";
			sortOrder = Constant.TIME;
			pageNum = 1;
		}
		boolean sameCondition = false;
		boolean samePageWindow = false;
		if (sessionQueryString != null) {
			//判断查询条件和排序方式是否相同
			sameCondition = sessionQueryString.split("pageNum=")[0].equals(queryString.split("pageNum=")[0]);
			//判断翻页前后页数是否在同一个页面缓存窗口（比如10页）
			Integer sessionPageNum = (Integer) session.get(Constant.S_SCHEME_PAGE_NUM);
			samePageWindow = sessionPageNum!=null && (sessionPageNum-1)/Constant.PAGE_RETURN==(pageNum-1)/Constant.PAGE_RETURN;
		}
		session.put(Constant.S_SCHEME_QUERY_STRING, queryString);
		session.put(Constant.S_SCHEME_PAGE_NUM, pageNum);
		
		if (!sameCondition || !samePageWindow) {
			if (!sameCondition) {
				session.put(Constant.S_SCHEME_RESULT_AMOUNT, schemeService.getFilterResultLength(selectedBrands, selectedPrices, selectedTypes));
			}
			List<Scheme> result = null;
			switch (sortOrder) {
			case Constant.TIME:
				result = schemeService.filter(selectedBrands, selectedPrices, selectedTypes, SortType.NEW, SortWay.DESC, pageNum);
				break;
			case Constant.SALE:
				result = schemeService.filter(selectedBrands, selectedPrices, selectedTypes, SortType.HOT, SortWay.DESC, pageNum);
				break;
			case Constant.PRICE_LOW:
				result = schemeService.filter(selectedBrands, selectedPrices, selectedTypes, SortType.PRICE, SortWay.ASC, pageNum);
				break;
			case Constant.PRICE_HIGH:
				result = schemeService.filter(selectedBrands, selectedPrices, selectedTypes, SortType.PRICE, SortWay.DESC, pageNum);
				break;
			case Constant.SCORE_USER:
				result = schemeService.filter(selectedBrands, selectedPrices, selectedTypes, SortType.W_SCORE, SortWay.DESC, pageNum);
				break;
			case Constant.SCORE_PRO:
				result = schemeService.filter(selectedBrands, selectedPrices, selectedTypes, SortType.Z_SCORE, SortWay.DESC, pageNum);
				break;
			//Constant.PERFORMANCE
			default:
				result = schemeService.filter(selectedBrands, selectedPrices, selectedTypes, SortType.PERFORMANCE, SortWay.DESC, pageNum);
				break;
			}
			session.put(Constant.S_SCHEME_RESULT, result);
		}
		
		return SUCCESS;
	}
	
}
